/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        if(head==nullptr||head->next==nullptr||head->next->next==nullptr) return;
        ListNode* slow=head;
        ListNode* fast=head;
        while(fast&&fast->next)
        {
            slow=slow->next;
            fast=fast->next->next;
        }
        ListNode* newhead=new ListNode(0);
        ListNode* cur=slow->next;
        //newhead->next=cur;
        slow->next=nullptr;
        while(cur)
        {
            ListNode* next=cur->next;
            cur->next=newhead->next;
            newhead->next=cur;
            cur=next;
        }
        ListNode* ret=new ListNode(0);
        ret->next=head;
        ListNode* prev=ret;
        ListNode* first=head;
        ListNode* second=newhead->next;
        while(first&&second)
        {
            prev->next=first;
            first=first->next;
            prev=prev->next;
            prev->next=second;
            prev=prev->next;
            second=second->next;
        }
        while(first)
        {
            prev->next=first;
            first=first->next;
            prev=prev->next;
        }
        while(second)
        {
            prev->next=second;
            prev=prev->next;
            second=second->next;
        }

        delete newhead;
        delete ret;
    }
};
